package com.lineage.data.npc.shop;

import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.EzpayReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NPC_Ezpay extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(NPC_Ezpay.class);
  
  public static NpcExecutor get() {
    return new NPC_Ezpay();
  }
  
  public int type() {
    return 3;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    try {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "send_s_0"));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
    boolean isCloseList = false;
    if (cmd.equals("up")) {
      int page = pc.get_other().get_page() - 1;
      showPage(pc, npc, page);
    } else if (cmd.equals("dn")) {
      int page = pc.get_other().get_page() + 1;
      showPage(pc, npc, page);
    } else if (cmd.equalsIgnoreCase("1")) {
      (pc.get_otherList()).SHOPLIST.clear();
      Map<Integer, int[]> info = EzpayReading.get().ezpayInfo(pc.getAccountName().toLowerCase());
      if (info.size() <= 0) {
        isCloseList = true;
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aD并没有查询到您的相关商品记录!!"));
      } else {
        pc.get_other().set_page(0);
        int index = 0;
        Iterator<Integer> iterator = info.keySet().iterator();
        while (iterator.hasNext()) {
          Integer key = iterator.next();
          int[] value = info.get(key);
          if (value != null) {
            (pc.get_otherList()).SHOPLIST.put(Integer.valueOf(index), value);
            index++;
          } 
        } 
        showPage(pc, npc, 0);
      } 
    } else if (cmd.equalsIgnoreCase("2")) {
      Map<Integer, int[]> info = EzpayReading.get().ezpayInfo(pc.getAccountName().toLowerCase());
      if (info.size() <= 0) {
        isCloseList = true;
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aG并没有查询到您的相关商品记录!!"));
      } else {
        Iterator<Integer> iterator2 = info.keySet().iterator();
        while (iterator2.hasNext()) {
          Integer key2 = iterator2.next();
          int[] value2 = info.get(key2);
          int id = value2[0];
          int itemid = value2[1];
          int count = value2[2];
          if (EzpayReading.get().update(pc.getAccountName(), id, pc.getName(), 
              pc.getNetConnection().getIp().toString()))
            createNewItem(pc, npc, itemid, count); 
        } 
      } 
      isCloseList = true;
    } else {
      isCloseList = true;
    } 
    if (isCloseList)
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId())); 
  }
  
  private static void showPage(L1PcInstance pc, L1NpcInstance npc, int page) {
    Map<Integer, int[]> list = (pc.get_otherList()).SHOPLIST;
    int allpage = list.size() / 10;
    if (page > allpage || page < 0)
      page = 0; 
    if (list.size() % 10 != 0)
      allpage++; 
    pc.get_other().set_page(page);
    int showId = page * 10;
    StringBuilder stringBuilder = new StringBuilder();
    int key = showId;
    while (key < showId + 10) {
      int[] info = list.get(Integer.valueOf(key));
      if (info != null) {
        L1Item itemtmp = ItemTable.get().getTemplate(info[1]);
        if (itemtmp != null)
          stringBuilder.append(String.valueOf(String.valueOf(itemtmp.getName())) + "(" + info[2] + "),"); 
      } 
      key++;
    } 
    String[] clientStrAry = stringBuilder.toString().split(",");
    if (allpage == 1) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "send_s_1", clientStrAry));
    } else if (page < 1) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "send_s_3", clientStrAry));
    } else if (page >= allpage - 1) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "send_s_4", clientStrAry));
    } else {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "send_s_2", clientStrAry));
    } 
  }
  
  private static void createNewItem(L1PcInstance pc, L1NpcInstance npc, int item_id, long count) {
    try {
      if (pc == null)
        return; 
      L1ItemInstance item = ItemTable.get().createItem(item_id);
      if (item != null) {
        item.setCount(count);
        item.setIdentified(true);
        pc.getInventory().storeItem(item);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fW" + npc.getNameId() + "给你" + item.getLogName()));
      } else {
        _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
